﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using JControlEx.Common;

namespace JControlEx
{
    /// <summary>
    /// Interaction logic for ViewObjectWindow.xaml
    /// </summary>
    public partial class ViewObjectWindow : Window
    {
        public ViewObjectWindow()
        {
            InitializeComponent();
            this.objectViewer.Me = this;
        }

        public ObjectViewer ObjectViewer
        {
            get { return this.objectViewer; }
        }

        public bool IsReadOnly 
        {
            set
            {

            }
        }

        public Type ParentType { get; set; }

        public MemberProperty MemberProperty { get; set; }

        private object _selectedObject = null;
        public object SelectedObject
        {
            get { return _selectedObject; }
            set
            {
                _selectedObject = value;
                this.comboBoxResourceList.GetBindingExpression(ComboBox.SelectedItemProperty).UpdateTarget();
                this.objectViewer.GetBindingExpression(ObjectViewer.DataContextProperty).UpdateTarget();
            }
        }

        public List<Object> ResourceList
        {
            get;
            set;
        }

        private Type _selectedItemType = null;
        public Type SelectedItemType
        {
            get { return _selectedItemType; }
            set
            {
                _selectedItemType = value;
                SelectedObject = Activator.CreateInstance(_selectedItemType);
                this.comboBoxTypes.GetBindingExpression(ComboBox.SelectedItemProperty).UpdateTarget();
                List<Object> resources = this.objectViewer.ResourceListProvider(ParentType, _selectedItemType, MemberProperty);
                ResourceList = resources;
                this.comboBoxResourceList.GetBindingExpression(ComboBox.ItemsSourceProperty).UpdateTarget();
            }
        }

        public List<Type> ItemTypes
        {
            get;
            set;
        }

        private void comboBoxResourceList_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void NullButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void NewButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            switch ((string)(sender as Button).Content)
            {
                case "OK":
                    DialogResult = true;
                    break;

                case "Cancel":
                    DialogResult = false;
                    break;

                default:
                    DialogResult = null;
                    break;
            }
            this.Close();
        }
    }
}
